﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XNADota.Input;
using XNADota.ContentPipeline.Data;
using System.Diagnostics;

namespace XNADota.UI
{
    public class Button : UIElement, IInputHandler
    {
        public const int MaxButtonStateCount = 2;

        private int _CurrentStateIndex;
        private Label[] _ButtonStates;

        private int _X;
        private int _Y;
        private int _Width;
        private int _Height;
        private int _OffsetX;
        private int _OffsetY;

        public Button(Game game, int X, int Y, int Width, int Height)
            : base(game)
        {
            _CurrentStateIndex = -1;
            _ButtonStates = new Label[MaxButtonStateCount];

            _X = X;
            _Y = Y;
            _Width = Width;
            _Height = Height;
        }

        public void Clear()
        {
            this.Visible = false;
        }

        public void AddLabel(string iconName, int offsetX, int offsetY, int index)
        {
            LabelSetting setting = new LabelSetting();

            _OffsetX = offsetX;
            _OffsetY = offsetY;

            setting.X = _X + _OffsetX;
            setting.Y = _Y + _OffsetY;
            setting.Width = _Width;
            setting.Height = _Height;

            setting.SX = 0;
            setting.SY = 0;
            setting.SW = 1;
            setting.SH = 1;

            setting.TextureFileName = iconName;

            _ButtonStates[index] = new Label(_Game, setting);

            if (_CurrentStateIndex == -1)
                _CurrentStateIndex = 0;
        }

        public void SetLabel(string iconName, int index)
        {
            LabelSetting setting = _ButtonStates[index].Setting;

            if (setting.TextureFileName.Equals(iconName) == false)
            {
                setting.TextureFileName = iconName;

                _ButtonStates[index] = new Label(_Game, setting);
            }
        }

        public void Update(GameTime gameTime)
        {

        }

        public override void Draw(GameTime gameTime)
        {
            if (this.Visible)
            {
                if (_CurrentStateIndex != -1 && _ButtonStates[_CurrentStateIndex] != null)
                    _ButtonStates[_CurrentStateIndex].Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        public void ProcessInput(GameTime gameTime, InputState inputState)
        {
            if (inputState.IsLeftButtonClick)
            {
                int x = (int)inputState.CursorPostion.X - _OffsetX;
                int y = (int)inputState.CursorPostion.Y - _OffsetY;
                if (_X <= x && x <= _X + _Width - 1 && _Y <= y && y <= _Y + _Height - 1)
                {
                    _CurrentStateIndex = _CurrentStateIndex + 1;
                    if (_CurrentStateIndex >= _ButtonStates.Length)
                        _CurrentStateIndex = 0;
                }
            }
        }

        private void PrintDubugInfo()
        {
            Debug.WriteLine(_X + " " + _Y);
        }
    }
}
